﻿using MenusHunter.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MenusHunter.Core.Helper
{
    public class AccountsHelper
    {
        /// <summary>
        /// Chek Login
        /// </summary>
        /// <param name="acc"></param>
        /// <returns></returns>
        public static Account CheckLogin(Account acc)
        {
            using(var db = new MenusHunterDataContext())
            {
                return (from a in db.Accounts where a.Email == acc.Email && a.Password == acc.Password && acc.Status==1 select a).SingleOrDefault();
            }
        }

        /// <summary>
        /// Get account by email
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public static Account GetAccountByEmail(string email)
        {
            using (var db = new MenusHunterDataContext())
            {
                return (from a in db.Accounts where a.Email == email && a.Status==1 select a).SingleOrDefault();
            }  
        }

        /// <summary>
        /// New Account
        /// </summary>
        /// <param name="acc"></param>
        /// <returns></returns>
        public static bool NewAccount(Account acc)
        {
            using (var db = new MenusHunterDataContext())
            {
                db.Accounts.InsertOnSubmit(acc);

                return acc.Id > 0;
            }
        }

        /// <summary>
        /// Update account info
        /// </summary>
        /// <param name="acc"></param>
        /// <returns></returns>
        public static bool UpdatAccount(Account acc)
        {
            using (var db = new MenusHunterDataContext())
            {
                db.Accounts.InsertOnSubmit(acc);
                db.SubmitChanges();

                return true;
            }
        }

        /// <summary>
        /// Delete Account
        /// </summary>
        /// <param name="acc"></param>
        /// <returns></returns>
        public static bool DeleteAccount(Account acc)
        {
            using (var db = new MenusHunterDataContext())
            {
                db.Accounts.DeleteOnSubmit(acc);
                db.SubmitChanges();
                return true;
            }
        }
    }
}
